/**
 * Description:
 * User:岁亮亮
 * Date:2025-03-05
 * TIME:9:07
 */
public class Sort0305 {
    private void swap(int[] array,int i,int j){
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
      //直接插入排序
    public void instersionSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int flag = array[i];
            for (int j = i; j >= 0; j--) {
                if((j == 0) || array[j-1] < flag){
                    array[j] = flag;
                    break;
                }
                else{
                    array[j] = array[j-1];
                }

            }

        }
    }
    public void instersionSort2(int[] array){
        for (int i = 1; i < array.length; i++) {
            int rel = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if(array[j] > rel){
                    array[j+1] =array[j];
                }
                else{
                    break;
                }
            }
            array[j+1] = rel;

        }
    }

    //希尔排序
    public void shellSort(int[] array){
        int gap = array.length;
        while(gap > 1){
            gap = gap / 2;
            instersionSortToShell(array,gap);
        }
    }

    private void instersionSortToShell(int[] array, int gap) {
        for (int i = gap; i < array.length; i+=gap) {
            int rel = array[i];
            int j = i - gap;
            for(; j >= 0; j -= gap){
                if(array[j] > rel){
                    array[j+gap] = array[j];
                }
                else {
                    break;
                }
            }
            array[j+gap] = rel;
        }
    }
    //冒泡排序
    public void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - 1 -i; j++) {
                if(array[j] > array[j+1]){
                    flag = true;
                    swap(array,j,j+1);
                }
            }
            if(!flag){
                break;
            }

        }
    }
    public void quickSort(int[] array){
        realQuickSort(array,0,array.length-1);
    }

    private void realQuickSort(int[] array, int left, int right) {
        if(left >= right){
            return;
        }
        int pivotIndex = patition(array,left,right);
        realQuickSort(array,left,pivotIndex);
        realQuickSort(array,pivotIndex+1,right);
    }
    private int middleOfThree(int[] array,int left,int right){
        int mid = (left + right)/2;
        if(array[left] < array[right]){
            if(array[mid] < array[left]){
                return left;
            }
            else if(array[mid] > array[right]){
                return right;
            }
            else {
                return mid;
            }
        }
        return -1;
    }


    private int patition(int[] array, int left, int right) {
        int rel = left;
        int pivot = array[left];
        while(left < right){
            while(left < right && array[right] >= pivot){
                right--;
            }
            while(left < right && array[left] <= pivot){
                left++;
            }
            swap(array,left,right);
        }
       swap(array,left,rel);
        return left;
    }
    private int digHole(int[] array,int left,int right){
        int pivot = array[left];
        while (left < right){
            while(left < right && array[right] >= pivot){
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= pivot){
                left++;
            }
            array[right] = array[left];

        }
        array[left] = pivot;
        return left;
    }
    private int pointer(int[] array,int left,int right){
        int pivot = array[left];
        int prev = left;
        int cur = prev + 1;
        while(cur <= right){
            if(array[cur] <= pivot && array[++prev] != array[cur]){
                swap(array,prev,cur);
            }
            cur++;
        }
        array[prev] = pivot;
        return prev;

    }


}
